import java.io.File;
import java.io.FileOutputStream;

/**
 * Algorithm for Item Outlier detection using a Threshold.
 * The threshold was chosen on the bases of a histogram
 * @author Joost Huizinga
 *
 */
public class AIOT extends Scheduler implements Runnable {
	static int THRESHOLD = 175000;
	
	int start;
	int stop;
	byte[] BLOCK;

	public AIOT(int start, int stop) {
		this.start = start;
		this.stop = stop;
	}

	void writeBlock() {
		String FILE_NAME = String.format("%s%07d.bin", BLOCK_PATH, start);		File out = new File(FILE_NAME);
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(out);
			fos.write(BLOCK);
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("Done writing: " + FILE_NAME + " @ "
				+ this.getClass());
	}

	@Override
	public void run() {
		int ilen = USERB[0].ilen;
		BLOCK = new byte[(stop - start) * ilen];

		int c = 0;
		for (int bUserID = start; bUserID < stop; bUserID++) {
			for (int i = 0; i < ilen; i++) {
				BLOCK[c * ilen + i] = predict(ITEMA[USERB[bUserID].items[i]]);
			}
			c++;
		}

		writeBlock();
	}
	
	byte predict(Item item){
		if(item.ulen > THRESHOLD){
			return 1;
		} else {
			return 0;
		}
	}
}